<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link rel="stylesheet" type="text/css" href="../../css/index.css" />
		<script src="../../js/vue.min.js" type="text/javascript" charset="utf-8"></script>
		<script src="../../js/vant.min.js" type="text/javascript" charset="utf-8"></script>
		<script src="../../js/ajax.js" type="text/javascript" charset="utf-8"></script>
		<script src="../../js/xback.js" type="text/javascript" charset="utf-8"></script>
		<script src="html5plus://ready"></script>
		<title></title>
		<style type="text/css">
			body {
				background-color: #E5E5E5;
			}
			
			#header{
				background-color: red;
				height: 50px;
			}
			.van-nav-bar__title {
				color: white;
				font-size: 15px;
				padding-top: 5px;
			}
			.van-nav-bar .van-icon {
				color: white;
				padding-top: 10px;
			}
			.van-nav-bar__text {
				color: white;
				padding-top: 10px;
			}
			.van-address-list {
				height: 100%;
				padding-bottom: 10px;
				box-sizing: border-box;
			}
			.van-button--small {
				margin-left: -30px;
			}
			p {
				font-size: 16px;
			}
		</style>
	</head>

	<body>
		<div id="app">
			<van-row>
				<van-nav-bar :title="title" left-text="返回" left-arrow id="header" @click-left="back"></van-nav-bar>
			</van-row>
			<van-row>
				<van-cell-group>
					<van-field v-model="phone" center clearable label="手机号" placeholder="请输入手机号">
					</van-field>
					<van-field v-model="phone_code" center clearable label="短信验证码" placeholder="请输入短信验证码">
					<van-button slot="button" size="small" type="danger" @click="Sendcode">发送验证码</van-button>
					</van-field>
					<van-field v-model="password" center clearable label="新密码" placeholder="输入新密码">
					</van-field>
					<van-field v-model="repassword" center clearable label="再次输入" placeholder="再次输入新密码">
					</van-field>
				</van-cell-group>
				<!--密码-->
				<van-col span="16" offset="4">
					<van-button type="danger" style="width: 100%;"  @click="Updatapwd">下一步</van-button>
				</van-col>
			</van-row>
		</div>
		<script type="text/javascript">
			var app = new Vue({
				el: "#app",
				data: {
					password: "",
					repassword:"",
					phone_code: "",
					phone:"",
					next:"下一步",
					key:"",
					title:"修改密码",
				},
				mounted() {
					var _this=this;
					plus.key.addEventListener("backbutton",function(){  
						var wv = plus.webview.currentWebview();
						plus.webview.close(wv.id);
					});  
					if(localStorage.getItem("keys")==null){
						_this.title="忘记密码";	
					};
				},
				methods:{
					back:function(){
						plus.webview.close("Resetpwd");
					},
					Sendcode:function(){
						var _this=this;
						_this.key=localStorage.getItem("keys");
						if(localStorage.getItem("keys")==null){
							console.log(_this.phone);
							ajax({
								url:"http://dsapi.ysd3g.com/API/SendForgotPasswordPhoneCode",//忘记密码
								dataType:"JSONP",
								data:{
									phone:_this.phone
								},	
								success:function(res){
									console.log(res)
									if(res.Error){
										app.$toast(res.Msg);
									}
									else{
										app.$toast(res.Code);
										_this.phone_code=res.Code;
									}
								}
							});
						}else{
							ajax({
								url:"http://dsapi.ysd3g.com/API/SendChangePasswordCode",//修改密码
								dataType:"JSONP",
								data:{
									key:_this.key
								},	
								success:function(res){
									// console.log(res)
									if(res.Error){
										app.$toast(res.Msg);
									}
									else{
										app.$toast(res.Code);
										_this.phone_code=res.Code;
										
									}
								}
							});
						}
					},
					Updatapwd(){
						var _this=this;
						if(_this.password!=_this.repassword){
							_this.$toast("两次密码填写不一致");
						}
						else{
							if(localStorage.getItem("keys")==null){
								ajax({
									url:"http://dsapi.ysd3g.com/api/ValidatePhone",
									dataType:"jsonp",
									data:{
										phone:_this.phone,
										phoneCode:_this.phone_code
									},
									success(res){
										if (res.Error) {
											vm.$toast.fail(res.Msg);
										} else{
											ajax({
												url: "http://dsapi.ysd3g.com/api/ResetPassword",
												dataType: "jsonp",
												data: {
													phone: _this.phone,
													password: _this.password
												},
												success(res) {
													console.log(res);
													if (res.Error) {
														_this.$toast.fail(res.Msg);
													} else{
														_this.$toast.success("修改成功,即将跳转到登录页面");
														setTimeout(function(){
															if(plus.webview.getWebviewById("login")==null){
																plus.webview.create("./login.html","login");
																var vm=plus.webview.currentWebview();
																plus.webview.close(vm.id);
																plus.webview.show("login");
															}
															else{
																var vm=plus.webview.currentWebview();
																plus.webview.close(vm.id);
																plus.webview.show("login");
															}
														},1000)
													}
												}
											});
										}
									}
								});
							}else{
								ajax({
									type:"post",
									url:"http://dsapi.ysd3g.com/API/ChangePassword",
									dataType:"JSONP",
									data:{
										key:_this.key,
										password:_this.password,
										phoneCode:_this.phone_code
									},
									success:function(res){
										if(res.Error){
											_this.$toast.fail("修改失败");
										}
										else{
											_this.$toast.success("修改成功,即将跳转...");
											setTimeout(function(){
												if(plus.webview.getWebviewById("login")==null){
													plus.webview.create("./login.html","login");
													var vm=plus.webview.currentWebview();
													plus.webview.close(vm.id);
													plus.webview.show("login");
												}
												else{
													var vm=plus.webview.currentWebview();
													plus.webview.close(vm.id);
													plus.webview.show("login");
												}
											},1000)
										}
									}
								});
							}
						}
					},
				}
			});
		</script>
	</body>

</html>